package com.ruoyi.web.controller.fs;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.fs.domain.*;
import com.ruoyi.fs.service.IFsBatchService;
import com.ruoyi.fs.service.IFsEntrustService;
import com.ruoyi.fs.service.IFsEvidenceRuleItemService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fs.service.IFsEvidenceRuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 证据规则Controller
 *
 * @author hero
 * @date 2024-08-25
 */
@RestController
@RequestMapping("/fs/rule")
public class FsEvidenceRuleController extends BaseController {
    @Autowired
    private IFsEvidenceRuleService fsEvidenceRuleService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private IFsBatchService fsBatchService;
    @Autowired
    private IFsEvidenceRuleItemService fsEvidenceRuleItemService;
    @Autowired
    private IFsEntrustService fsEntrustService;


    /**
     * 查询证据规则列表
     */
    @PreAuthorize("@ss.hasPermi('fs:rule:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsEvidenceRule fsEvidenceRule) {
        startPage();
        List<FsEvidenceRule> list = fsEvidenceRuleService.selectFsEvidenceRuleList(fsEvidenceRule);
        for (int i = 0; i < list.size(); i++) {
            FsEvidenceRule rule = list.get(i);
            FsEntrust zf = fsEntrustService.selectFsEntrustById(rule.getZfId());
            if (zf != null) {
                rule.setZfName(zf.getName());
            }
            FsBatch batch = fsBatchService.selectFsBatchById(rule.getBatchId());
            if (batch != null) {
                rule.setBatchName(batch.getBatchName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询资方批次信息列表
     */
    @PreAuthorize("@ss.hasPermi('fs:rule:list')")
    @GetMapping("/listBatch")
    public AjaxResult listBatch(@RequestParam(value = "zfId", required = true) Long zfId) {
        FsBatch batch = new FsBatch();
        batch.setZfid(zfId);
        List<FsBatch> list = fsBatchService.selectFsBatchList(batch);
        return success(list);
    }


    /**
     * 导出证据规则列表
     */
    @PreAuthorize("@ss.hasPermi('fs:rule:export')")
    @Log(title = "证据规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsEvidenceRule fsEvidenceRule) {
        List<FsEvidenceRule> list = fsEvidenceRuleService.selectFsEvidenceRuleList(fsEvidenceRule);
        ExcelUtil<FsEvidenceRule> util = new ExcelUtil<FsEvidenceRule>(FsEvidenceRule.class);
        util.exportExcel(response, list, "证据规则数据");
    }

    /**
     * 获取证据规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:rule:query')")
    @GetMapping(value = "/{ruleId}")
    public AjaxResult getInfo(@PathVariable("ruleId") Long ruleId) {
        return success(fsEvidenceRuleService.selectFsEvidenceRuleByRuleId(ruleId));
    }

    /**
     * 新增证据规则
     */
    @PreAuthorize("@ss.hasPermi('fs:rule:add')")
    @Log(title = "证据规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsEvidenceRule fsEvidenceRule) {
        String ajlx = dictDataService.selectDictLabel("fs_gw_ajlx", fsEvidenceRule.getAjlx());
        String sqlx = dictDataService.selectDictLabel("fs_gw_sqlx", fsEvidenceRule.getSqlx());
        fsEvidenceRule.setAjlxMc(ajlx);
        fsEvidenceRule.setSqlxMc(sqlx);
        fsEvidenceRuleService.insertFsEvidenceRule(fsEvidenceRule);
        Long ruleId = fsEvidenceRule.getRuleId();
        if (fsEvidenceRule.getCopyId() != null) {
            FsEvidenceRuleItem old = new FsEvidenceRuleItem();
            old.setRuleId(fsEvidenceRule.getCopyId());
            List<FsEvidenceRuleItem> oldItems = fsEvidenceRuleItemService.selectFsEvidenceRuleItemList(old);
            for (int i = 0; i < oldItems.size(); i++) {
                FsEvidenceRuleItem item = oldItems.get(i);
                item.setItemId(null);
                item.setRuleId(ruleId);
                fsEvidenceRuleItemService.insertFsEvidenceRuleItem(item);
            }
        }
        return success();
    }

    /**
     * 修改证据规则
     */
    @PreAuthorize("@ss.hasPermi('fs:rule:edit')")
    @Log(title = "证据规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsEvidenceRule fsEvidenceRule) {
        String ajlx = dictDataService.selectDictLabel("fs_gw_ajlx", fsEvidenceRule.getAjlx());
        String sqlx = dictDataService.selectDictLabel("fs_gw_sqlx", fsEvidenceRule.getSqlx());
        fsEvidenceRule.setAjlxMc(ajlx);
        fsEvidenceRule.setSqlxMc(sqlx);
        return toAjax(fsEvidenceRuleService.updateFsEvidenceRule(fsEvidenceRule));
    }

    /**
     * 删除证据规则
     */
    @PreAuthorize("@ss.hasPermi('fs:rule:remove')")
    @Log(title = "证据规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public AjaxResult remove(@PathVariable Long[] ruleIds) {
        return toAjax(fsEvidenceRuleService.deleteFsEvidenceRuleByRuleIds(ruleIds));
    }
}
